unit ColumnsGui;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,

  InventionCalc, StdCtrls, ExtCtrls, CheckLst, Buttons, GlobalRecordsAndEnums;

type
  TfrmColumns = class(TForm)
    CheckListBox: TCheckListBox;
    lbl1: TLabel;
    lbl2: TLabel;
    btnOK: TButton;
    btnDown: TSpeedButton;
    btnUp: TSpeedButton;
    lblReset: TLabel;
    btnHelp: TButton;
    procedure btnOKClick(Sender: TObject);
    procedure btnUpClick(Sender: TObject);
    procedure btnDownClick(Sender: TObject);
    procedure lblResetClick(Sender: TObject);
    procedure btnHelpClick(Sender: TObject);
  private
    inventionCalc: TInventionCalc;
//   columns: array [0..9] of Integer;
    // Indicates the order of the columns. It holds column IDs, which represent the following:
    // 0: ID
    // 1: Name
    // 2: Invention Type
    // 3: Datacore 1
    // 4: Datacore 2
    // 5: T2 item 1
    // 6: T2 item 2
    // 7: Invention cost
    // 8: Build cost
    // 9: Race
    // 10: Build profit
    // So, columns[3] = 5, means that on position 3 (the 4th position) the column is 'T2 item 1'.
  public
//    PositionResult: array [0..9] of Integer;
//    ShowResult: array [0..9] of Boolean;

    constructor Create(AOwner: TComponent; invCalc: TInventionCalc); reintroduce;
  end;

var
  frmColumns: TfrmColumns;

implementation

{$R *.dfm}

uses
  ExtActns;

constructor TfrmColumns.Create(AOwner: TComponent; invCalc: TInventionCalc);
var
  i: Integer;
  columns: Array [0..10] of EColumn;
  addedcolumns: Array [0..10] of Boolean;
  // Array that indicates whether or not a column has been added to the form. Indices:
  // 0: COL_ID
  // 1: COL_Name
  // 2: COL_InventionType
  // 3: COL_Datacore1
  // 4: COL_Datacore2
  // 5: COL_T2Item1
  // 6: COL_T2Item2
  // 7: COL_InventionCcost
  // 8: COL_BuildCost
  // 9: COL_Race
  // 10: COL_T1ItemProfit
begin
  Inherited Create(AOwner);
  inventionCalc := invCalc;
  for i := 0 to 10 do begin
    columns[i] := inventionCalc.UserSettings.Settings.Columns[i];
    addedcolumns[i] := false;
  end;

  // add selected columns to the top of the list
  for i := 0 to 10 do begin
    case columns[i] of
      COL_ID:              begin
                             CheckListBox.Items.AddObject('ID', TObject(COL_ID));
                             addedcolumns[0] := true;
                           end;
      COL_Name:            begin
                             CheckListBox.Items.AddObject('Name', TObject(COL_Name));
                             addedcolumns[1] := true;
                           end;
      COL_InventionType:   begin
                             CheckListBox.Items.AddObject('Invention Type', TObject(COL_InventionType));
                             addedcolumns[2] := true;
                           end;
      COL_Datacore1:       begin
                             CheckListBox.Items.AddObject('Datacore 1', TObject(COL_Datacore1));
                             addedcolumns[3] := true;
                           end;
      COL_Datacore2:       begin
                             CheckListBox.Items.AddObject('Datacore 2', TObject(COL_Datacore2));
                             addedcolumns[4] := true;
                           end;
      COL_T2Item1:         begin
                             CheckListBox.Items.AddObject('T2 Item 1', TObject(COL_T2Item1));
                             addedcolumns[5] := true;
                           end;
      COL_T2Item2:         begin
                             CheckListBox.Items.AddObject('T2 Item 2', TObject(COL_T2Item2));
                             addedcolumns[6] := true;
                            end;
      COL_InventionProfit: begin
                             CheckListBox.Items.AddObject('Invention Profit', TObject(COL_InventionProfit));
                             addedcolumns[7] := true;
                           end;
      COL_BuildCost:       begin
                             CheckListBox.Items.AddObject('Build Cost', TObject(COL_BuildCost));
                             addedcolumns[8] := true;
                           end;
      COL_Race:            begin
                             CheckListBox.Items.AddObject('Race', TObject(COL_Race));
                             addedcolumns[9] := true;
                           end;
      COL_T1ItemProfit:    begin
                             CheckListBox.Items.AddObject('Build Profit', TObject(COL_T1ItemProfit));
                             addedcolumns[10] := true;
                           end;

    end;
//    if (columns[i] <> COL_None) then CheckListBox.Checked[i] := true;
  end;

  for i := 0 to CheckListBox.Items.Count -1 do begin
    CheckListBox.Checked[i] := true;
  end;

  // add remaining (unselected) columns
  for i := 0 to 10 do begin
    if (not addedcolumns[i]) then begin
      case i of
        0: CheckListBox.Items.AddObject('ID', TObject(COL_ID));
        1: CheckListBox.Items.AddObject('Name', TObject(COL_Name));
        2: CheckListBox.Items.AddObject('Invention Type', TObject(COL_InventionType));
        3: CheckListBox.Items.AddObject('Datacore 1', TObject(COL_Datacore1));
        4: CheckListBox.Items.AddObject('Datacore 2', TObject(COL_Datacore2));
        5: CheckListBox.Items.AddObject('T2 Item 1', TObject(COL_T2Item1));
        6: CheckListBox.Items.AddObject('T2 Item 2', TObject(COL_T2Item2));
        7: CheckListBox.Items.AddObject('Invention Profit', TObject(COL_InventionProfit));
        8: CheckListBox.Items.AddObject('Build Cost', TObject(COL_BuildCost));
        9: CheckListBox.Items.AddObject('Race', TObject(COL_Race));
        10: CheckListBox.Items.AddObject('Build Profit', TObject(COL_T1ItemProfit));
      end;
    end;
  end;
end;

procedure TfrmColumns.btnHelpClick(Sender: TObject);
var
  run: TFileRun;
begin
  run := TFileRun.Create(self);
  run.FileName := 'help\Columns.html';
  run.Execute;
  run.Free;
end;

procedure TfrmColumns.btnOKClick(Sender: TObject);
var
  i, j: Integer;
begin
  for i := 0 to 10 do begin
    if (CheckListBox.Checked[i]) then
      inventionCalc.UserSettings.Settings.Columns[i] := EColumn(CheckListBox.Items.Objects[i])
    else begin
      inventionCalc.UserSettings.Settings.Columns[i] := COL_None;
    end;
  end;

  // condense array: shift all values <> COL_None to the front (so the array will look like [COL_ID, COL_Name, COL_Race, COL_None, COL_None, COL_None, etc.]
  for i := 0 to 10 do begin
    if (inventionCalc.UserSettings.Settings.Columns[i] = COL_None) then begin
      for j := i+1 to 10 do begin
        if (inventionCalc.UserSettings.Settings.Columns[j] <> COL_None) then begin
          inventionCalc.UserSettings.Settings.Columns[i] := inventionCalc.UserSettings.Settings.Columns[j];
          inventionCalc.UserSettings.Settings.Columns[j] := COL_None;
          break;
        end;
      end;
    end;
  end;
end;


procedure TfrmColumns.btnUpClick(Sender: TObject);
var
  i: Integer;
begin
  for i := 1 to 10 do begin
    if (CheckListBox.Selected[i]) then begin
      CheckListBox.Items.Move(i, i-1);
      CheckListBox.Selected[i-1] := true;
      break;
    end;
  end;
end;

procedure TfrmColumns.btnDownClick(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to 9 do begin
    if (CheckListBox.Selected[i]) then begin
      CheckListBox.Items.Move(i, i+1);
      CheckListBox.Selected[i+1] := true;
      break;
    end;
  end;
end;

procedure TfrmColumns.lblResetClick(Sender: TObject);
begin
  CheckListBox.Items.Clear;
  CheckListBox.Items.AddObject('ID', TObject(COL_ID));
  CheckListBox.Items.AddObject('Name', TObject(COL_Name));
  CheckListBox.Items.AddObject('Invention Type', TObject(COL_InventionType));
  CheckListBox.Items.AddObject('Datacore 1', TObject(COL_Datacore1));
  CheckListBox.Items.AddObject('Datacore 2', TObject(COL_Datacore2));
  CheckListBox.Items.AddObject('T2 Item 1', TObject(COL_T2Item1));
  CheckListBox.Items.AddObject('T2 Item 2', TObject(COL_T2Item2));
  CheckListBox.Items.AddObject('Invention Cost', TObject(COL_InventionProfit));
  CheckListBox.Items.AddObject('Build Cost', TObject(COL_BuildCost));
  CheckListBox.Items.AddObject('Race', TObject(COL_Race));
  CheckListBox.Items.AddObject('Build Profit', TObject(COL_T1ItemProfit));
  CheckListBox.Checked[1] := true;
  CheckListBox.Checked[7] := true;
end;

end.
